<?php 

// 验证码类
/**
* 
*/
class Code 
{
	public $widht;
	public $img;
	public $height;
	public $num=4;
	public $fontSize = 30;
	public $Pointnum =100;

	function __construct($widht=null,$height=null)
	{
		$this->widht = is_null($widht) ? $this->widht : $widht ;
		$this->height = is_null($height) ? $this->height : $height ;//初始化数据
		
	}
	public function produce(){

		$this->img = imageCreateTrueColor($this->widht,$this->height);
	
		$color = imagecolorallocate($this->img,255,119,53 );
		
		imagefill($this->img, 0, 0, $color);

		$this->_addFont();		
		$this->_addPoint();
		$this->_addLine();		
	}

	public function _addFont(){
		$str="1234567890qwertyuiopasdfghjklzxcvbnm";

		$widht = ($this->widht/$this->num);//每个$str所占区域宽
		$height = ($this->height+$this->fontSize)/2-2;//每个$str所占区域高

		$num=($this->widht/$this->num)-$this->fontSize;//每个$str之间的间隙


		$res = '';

		for ($i=0; $i < $this->num; $i++) { 

			$color = imagecolorallocate($this->img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));		
			
			$fonts =$str[mt_rand(0,35)];
			$res .=$fonts;
	
			imagettftext($this->img, $this->fontSize, mt_rand(15,25), $num, $height,$color, './font.ttf', $fonts);
			$num+=$widht;//座标按左下角开始计算

		}
	$_SESSION['res_code']=$res;//存到SESSION
	}
	public function _addPoint(){
		for ($i=0; $i <$this->Pointnum ; $i++) { 
			$color = imagecolorallocate($this->img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
			imagesetpixel($this->img, mt_rand(0,$this->widht),mt_rand(0,$this->height), $color);

		}	
	}
	public function _addLine(){
		for ($i=0; $i <$this->num ; $i++) { 
			$color = imagecolorallocate($this->img, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
			imageline($this->img, mt_rand(0,100), mt_rand(0,50), mt_rand(50,200), mt_rand(20,50), $color);
		}
		header('Content-type: image/png');

		imagepng($this->img);
	}





}



 ?>